﻿Public Partial Class actual
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub


    Private Function Gen_ReportActual(ByVal HeadReport As String, ByVal REF_PE As String, ByVal ship_name As String, ByVal Period_Time As String, ByVal LAYTIME As String, ByVal LOA As String, ByVal GRT As String, ByVal DWT As String, ByVal PO As String, ByVal Wharf As String, ByVal Body As String) As String
        Dim HTML As String = ""

        HTML = "<table width='100%' border='0' cellpadding='0' cellspacing='1'>"
        HTML &= "<tr ><td colspan='6'>" & HeadReport & "</td></tr>"
        HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"

        HTML &= "<tr ><td colspan='6'><table width='100%' border='0' cellpadding='0' cellspacing='1'>"
        HTML &= "<tr>"
        HTML &= "<td colspan='5' align='left'>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'><font class='White-Style' style='color:#000000'>REF. PE :</font>&nbsp;<font class='White-Style' style='color:#000000'>" & REF_PE & "</font></td>"
        HTML &= "</tr>"

        HTML &= "<tr>"
        HTML &= "<td style='width:140px;' align='left'><font class='White-Style' style='color:#000000'>SHIP's Name :</font>&nbsp;</td>"
        HTML &= "<td style='width:460px;' colspan='4' align='left'><font class='White-Style' style='color:#000000'>" & ship_name & "</font></td>"
        HTML &= "<td style='width:120px;' align='left'><font class='White-Style' style='color:#000000'>PEROD TIME :</font>&nbsp;</td>"
        HTML &= "<td style='width:180px;' align='left'><font class='White-Style' style='color:#000000'>" & Period_Time & "</font></td>"
        HTML &= "<td style='width:120px;' align='left'><font class='White-Style' style='color:#000000'>LAYTIME :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & LAYTIME & "</font></td>"
        HTML &= "</tr>"

        HTML &= "<tr>"
        HTML &= "<td style='width:140px;' align='left'><font class='White-Style' style='color:#000000'>L.O.A.(ft) :</font>&nbsp;</td>"
        HTML &= "<td style='width:460px;' colspan='4' align='left'><font class='White-Style' style='color:#000000'>" & LOA & "</font></td>"
        HTML &= "<td style='width:120px;' align='left'><font class='White-Style' style='color:#000000'>GRT :</font>&nbsp;</td>"
        HTML &= "<td style='width:180px;' align='left'><font class='White-Style' style='color:#000000'>" & GRT & "</font></td>"
        HTML &= "<td style='width:120px;' align='left'><font class='White-Style' style='color:#000000'>DWT :</font>&nbsp;</td>"
        HTML &= "<td align='left'><font class='White-Style' style='color:#000000'>" & DWT & "</font></td>"
        HTML &= "</tr>"

        HTML &= "<tr>"
        HTML &= "<td style='width:140px;' align='left'><font class='White-Style' style='color:#000000'>PO :</font>&nbsp;</td>"
        HTML &= "<td style='width:460px;' colspan='4' align='left'><font class='White-Style' style='color:#000000'>" & PO & "</font></td>"
        HTML &= "<td style='width:120px;' align='left'><font class='White-Style' style='color:#000000'>WHARF No. :</font>&nbsp;</td>"
        HTML &= "<td colspan='3' align='left'><font class='White-Style' style='color:#000000'>" & Wharf & "</font></td>"
        HTML &= "</tr>"

        HTML &= "</table></td></tr>"

        HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"

        HTML &= "<tr ><td colspan='6'>" & Body & "</td></tr>"

        HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"

        HTML &= "<tr>"
        HTML &= "<td colspan='6' align='left'><font class='White-Style' style='color:#000000'>REMARK</font>&nbsp;</td>"
        HTML &= "</tr>"

        HTML &= "<tr ><td style='height:3px;' colspan='6'><img src='../Image/space.gif' /></td></tr>"
        HTML &= "</table>"
        Return HTML
    End Function

    Private Sub FormatN(ByRef data As String)
        Try
            data = FormatNumber(data, 2)
        Catch ex As Exception

        End Try
    End Sub

    Private Function Gen_BodyActual(ByVal Vat As String, ByVal DSItem As DataTable) As String
        Dim HTML As String = ""
        HTML = "<table width='100%'  style='border-width: 0px;' border='1' cellpadding='0' cellspacing='0'>"
        Dim Value As Double = 0
        Dim ValueVat As Double = 0
        Dim WHT As Double = 0
        Dim row As Integer = 2

        HTML &= "<tr style='background-color:#2d9d1c;'>"
        HTML &= "<td style='width:140px;' valign='top'  align='left'><font class='White-Style' style='color:#000000'>CODE</font>&nbsp;</td>"
        HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>DESCRIPTION</font></td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'><font class='White-Style' style='color:#000000'>QTY</font>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'><font class='White-Style' style='color:#000000'>RATE/UNIT</font>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'><font class='White-Style' style='color:#000000'>QTY</font>&nbsp;</td>"
        HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>AMOUNT</font>&nbsp;</td>"
        HTML &= "</tr>"

        For Each dr As DataRow In DSItem.Rows
            If (CDbl(dr.Item("data4")) > 0.0) Then
                Dim MatName As String = dr.Item("MAT_NAME") & "<BR /> - " & dr.Item("name")
                If (dr.Item("MAT_NAME") = dr.Item("name")) Then
                    MatName = dr.Item("MAT_NAME")
                End If
                If (row Mod 2) Then
                    HTML &= "<tr>"
                Else
                    HTML &= "<tr style='background-color:#F0F9FB;'>"
                End If
                row += 1
                Dim data1 As String = dr.Item("data1")
                Dim data2 As String = dr.Item("data2")
                Dim data3 As String = dr.Item("data3")
                Dim data4 As String = dr.Item("data4")
                FormatN(data1)
                FormatN(data2)
                ' FormatN(data3)
                FormatN(data4)

                HTML &= "<td style='width:140px;' valign='top'  align='left'><font class='White-Style' style='color:#000000'>" & dr.Item("id") & "</font>&nbsp;</td>"
                HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>" & MatName & "</font></td>"
                HTML &= "<td style='width:80px;' valign='bottom' align='right'><font class='White-Style' style='color:#000000'>" & data1 & "</font>&nbsp;</td>"
                HTML &= "<td style='width:80px;' valign='bottom' align='right'><font class='White-Style' style='color:#000000'>" & data2 & "</font></td>"
                HTML &= "<td style='width:70px;' valign='bottom' align='center'><font class='White-Style' style='color:#000000'>" & data3 & "</font>&nbsp;</td>"
                HTML &= "<td align='right'  valign='bottom'><font class='White-Style' style='color:#000000'>" & data4 & "</font></td>"
                HTML &= "</tr>"
                Value = Value + CDbl(dr.Item("data4"))
                Dim dt As DataTable = clsGet.Get_MeterailWhithodingTax(dr.Item("id"))
                If (dt.Rows(0).Item("MAT_WHT") & "" = "01") Or (dt.Rows(0).Item("MAT_WHT") & "" = "02") Or (dt.Rows(0).Item("MAT_WHT") & "" = "03") Then
                    '1%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 1) / 100).ToString("#########0.00"))).ToString("###############0.00")
                ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "04") Or (dt.Rows(0).Item("MAT_WHT") & "" = "05") Then
                    '3%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 3) / 100).ToString("#########0.00"))).ToString("###############0.00")
                ElseIf (dt.Rows(0).Item("MAT_WHT") & "" = "06") Or (dt.Rows(0).Item("MAT_WHT") & "" = "22") Then
                    '5%
                    WHT = (WHT + CDbl(((CDbl(dr.Item("data4")) * 5) / 100).ToString("#########0.00"))).ToString("###############0.00")
                End If
            End If

        Next


        HTML &= "<tr>"
        HTML &= "<td style='width:140px;' rowspan='4' valign='top'  align='left'>&nbsp;</td>"
        HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>TOTAL SERVICE CHARGE    (EXCLUSIVE OF VAT)</font>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'>&nbsp;</td>"
        HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber(Value.ToString("#############0.00")) & "</font></td>"
        HTML &= "</tr>"

        ValueVat = ((Value * CDbl(Vat)) / 100).ToString("############.00")
        HTML &= "<tr>"
        HTML &= "<td style='width:280px;' align='left'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font class='White-Style' style='color:#000000'>ADD VAT " & Vat & "%</font>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'>&nbsp;</td>"
        HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber(ValueVat) & "</font></td>"
        HTML &= "</tr>"

        HTML &= "<tr>"
        HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>TOTAL SERVICE CHARGE    (INCLUSIVE OF VAT)</font>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'>&nbsp;</td>"
        HTML &= "<td align='right'><font class='White-Style' style='color:#000000'>" & FormatNumber((Value + ValueVat).ToString("###############.00")) & "</font></td>"
        HTML &= "</tr>"

        HTML &= "<tr>"
        HTML &= "<td style='width:280px;' align='left'><font class='White-Style' style='color:#000000'>" & ConvertMoneyToText((Value + ValueVat).ToString("###############.00")) & "</font>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:80px;' valign='bottom' align='right'>&nbsp;</td>"
        HTML &= "<td style='width:70px;' valign='bottom' align='center'>&nbsp;</td>"
        HTML &= "<td align='right'>&nbsp;</td>"
        HTML &= "</tr>"

        HTML &= "</table>"

        '   HTML &= Gen_Sum(Value.ToString("#############0.00"), ValueVat.ToString(), WHT.ToString())
        Return HTML
    End Function

    Private Function ConvertMoneyToText(ByVal Money As String) As String
        Dim Digit As String = Money.Split(".")(0)
        Dim SubMoney As String
        Try
            SubMoney = Money.Split(".")(0)
        Catch ex As Exception
            SubMoney = "00"
        End Try
        Dim COuntLength As Int16 = Digit.Length

        Dim TxtMoney As String = ""

        For i As Integer = 0 To COuntLength - 1
            If (COuntLength > 7) Then
                Select Case (Digit.ToString())
                    Case "9" : TxtMoney &= "เก้า"
                    Case "8" : TxtMoney &= "แปด"
                    Case "7" : TxtMoney &= "เจ็ด"
                    Case "6" : TxtMoney &= "หก"
                    Case "5" : TxtMoney &= "ห้า"
                    Case "4" : TxtMoney &= "สี่"
                    Case "3" : TxtMoney &= "สาม"
                    Case "2" : TxtMoney &= "สอง"
                    Case "1" : TxtMoney &= "หนึ่ง"
                End Select

                Select Case (COuntLength.ToString)
                    Case "13" : TxtMoney &= "ล้านล้าน"
                    Case "12" : TxtMoney &= "แสนล้าน"
                    Case "11" : TxtMoney &= "หมื่นล้าน"
                    Case "10" : TxtMoney &= "พันล้าน"
                    Case "9" : TxtMoney &= "ร้อยล้าน"
                    Case "8" : TxtMoney &= "สิบล้าน"
                End Select
            Else
                Select Case (Digit.ToString())
                    Case "9" : TxtMoney &= "เก้า"
                    Case "8" : TxtMoney &= "แปด"
                    Case "7" : TxtMoney &= "เจ็ด"
                    Case "6" : TxtMoney &= "หก"
                    Case "5" : TxtMoney &= "ห้า"
                    Case "4" : TxtMoney &= "สี่"
                    Case "3" : TxtMoney &= "สาม"
                    Case "2"
                        If (COuntLength.ToString() = "2") Then
                            TxtMoney &= "ยี่"
                        Else
                            TxtMoney &= "สอง"
                        End If

                    Case "1"
                        If (COuntLength.ToString() = "2") Then
                            TxtMoney &= "เอ็ด"
                        Else
                            TxtMoney &= "หนึ่ง"
                        End If
                End Select

                Select Case (COuntLength.ToString)
                    Case "7" : TxtMoney &= "ล้าน"
                    Case "6" : TxtMoney &= "แสน"
                    Case "5" : TxtMoney &= "หมื่น"
                    Case "4" : TxtMoney &= "พัน"
                    Case "3" : TxtMoney &= "ร้อย"
                    Case "2" : TxtMoney &= "สิบ"
                    Case "1" : TxtMoney &= "บาท"
                End Select
            End If
            Select Case (COuntLength.ToString())
                Case "13"
                    Digit = CInt(Digit) Mod 1000000000000
                Case "12"
                    Digit = CInt(Digit) Mod 100000000000
                Case "11"
                    Digit = CInt(Digit) Mod 10000000000
                Case "10"
                    Digit = CInt(Digit) Mod 1000000000
                Case "9"
                    Digit = CInt(Digit) Mod 100000000
                Case "8"
                    Digit = CInt(Digit) Mod 10000000
                Case "7"
                    Digit = CInt(Digit) Mod 1000000
                Case "6"
                    Digit = CInt(Digit) Mod 100000
                Case "5"
                    Digit = CInt(Digit) Mod 10000
                Case "4"
                    Digit = CInt(Digit) Mod 1000
                Case "3"
                    Digit = CInt(Digit) Mod 100
                Case "2"
                    Digit = CInt(Digit) Mod 10
                Case "1"
                    Digit = CInt(Digit) Mod 1
            End Select
            COuntLength -= 1
        Next
        If (SubMoney <> "00") Then
            If (SubMoney.Length = 2) Then
                Select Case (SubMoney.Substring(0, 1))
                    Case "9" : TxtMoney &= "เก้าสิบ"
                    Case "8" : TxtMoney &= "แปดสิบ"
                    Case "7" : TxtMoney &= "เจ็ดสิบ"
                    Case "6" : TxtMoney &= "หกสิบ"
                    Case "5" : TxtMoney &= "ห้าสิบ"
                    Case "4" : TxtMoney &= "สี่สิบ"
                    Case "3" : TxtMoney &= "สามสิบ"
                    Case "2" : TxtMoney &= "ยี่สิบ"
                    Case "1" : TxtMoney &= "สิบ"
                End Select
            End If
            Select Case (SubMoney.Substring(0, 1))
                Case "9" : TxtMoney &= "เก้าสตางค์"
                Case "8" : TxtMoney &= "แปดสตางค์"
                Case "7" : TxtMoney &= "เจ็ดสตางค์"
                Case "6" : TxtMoney &= "หกสตางค์"
                Case "5" : TxtMoney &= "ห้าสตางค์"
                Case "4" : TxtMoney &= "สี่สตางค์"
                Case "3" : TxtMoney &= "สามสตางค์"
                Case "2" : TxtMoney &= "สองสตางค์"
                Case "1"
                    If (SubMoney.Length = 2) Then
                        If (SubMoney.Substring(0, 1) = "0") Then
                            TxtMoney &= "หนึ่งสตางค์"
                        Else
                            TxtMoney &= "เอ็ดสตางค์"
                        End If

                    Else
                        TxtMoney &= "หนึ่งสตางค์"
                    End If
            End Select
        End If
        Return TxtMoney
    End Function


    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class